/**
 * This class represents a custom rss feed parser which is used to parse
 * rss feeds for all rss feeds except the events calendar module in the 
 * iWestern application. It is a subclass of RssFeedParser and uses 
 * methods / attributes from this class when parsing the rss feed.
 * @author Chirag Mistry
 */

package ca.uwo.android.mustangs;

import java.io.InputStream;

import org.xml.sax.Attributes;

import ca.uwo.android.rss.RssFeed;
import ca.uwo.android.rss.RssItem;
import ca.uwo.android.rss.SaxRssParser;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

public class MustangsVideoParser extends SaxRssParser {

	/**
	 * Used to tag log messages from this class
	 */
	private static final String TAG_NAME = "MustangsVideoParser";

	protected static final String MEDIA_NAMESPACE = "http://search.yahoo.com/mrss/";

	protected static final String XML_NAMESPACE = "http://www.w3.org/2005/Atom";

	protected static final String FEED = "feed";

	protected static final String ENTRY = "entry";

	protected static final String GROUP = "group";

	protected static final String PLAYER = "player";
	
	protected static final String URL = "url";

	/**
	 * Constructor for the rss feed parser
	 * 
	 * @param rssFeedURL
	 *            - the url of the rss feed to parse
	 */
	public MustangsVideoParser(String rssFeedURL) throws Exception {
		super(rssFeedURL);
	}

	/**
	 * This method parses the rss feed and returns an rss feed object containing
	 * all the feed items read in from the source
	 * 
	 * @return
	 */
	public RssFeed parse() throws Exception {

		// represents current item
		// being parsed
		final RssItem currentItem = new RssItem(); 
		
		//the root element
		RootElement root = new RootElement(XML_NAMESPACE, FEED); 

//		Log.i(TAG_NAME, "Got root element:  " + root);

		// rss feed object to store all rss items
		final RssFeed rssItems = new RssFeed();

		// get the entry element from root
		Element entry = root.getChild(XML_NAMESPACE, ENTRY);
		
//		Log.i(TAG_NAME, "Got entry: " + entry);

		String rssFeedText;

		// get the input stream to the
		// rss feed
		InputStream is = this.getInputStream();
		
		// convert special characters in
		// feed
		rssFeedText = convertSpecialChars(is); 

//		Log.i(TAG_NAME, "Rss feed text = " + rssFeedText);

		// get each (child) item element (in this case entry since it is an Atom feed)
		// from the channel and store it in the
		// rss feed object
		Element item = entry.getChild(MEDIA_NAMESPACE, GROUP);
//		Log.i(TAG_NAME, "Got item (group):  " + item);
		item.setEndElementListener(new EndElementListener() {
			public void end() {
//				Log.i(TAG_NAME, "Got media:group");
				rssItems.add(currentItem.copy());
			}
		});

		// parse and store the link for the video
		Element player = item.getChild(MEDIA_NAMESPACE, PLAYER);
//		Log.i(TAG_NAME, "Got player: " + player);
		
		//just need the url, which is an attribute of this element
		player.setStartElementListener(new StartElementListener() {

			@Override
			public void start(Attributes attributes) {
				String url = attributes.getValue(URL);
				//Log.i(TAG_NAME, "Found url " + url);
				currentItem.setLink(url);
				
			}
		});

		// parse and store the title of the rss feed item
		Element title = item.getChild(MEDIA_NAMESPACE, TITLE);
		//Log.i(TAG_NAME, "Got title: " + title);
		title.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				//Log.i(TAG_NAME, "Got media:title:  " + body);
				currentItem.setTitle(body);
			}
		});

		// parse the rss feed directly from the input stream using utf 8
		// encoding
		Xml.parse(rssFeedText, root.getContentHandler());

//		Log.i(TAG_NAME, "Finished parsing feed, the number of items is "
//				+ rssItems.size());

		return rssItems;
	}
}
